import request from "@/axios";
import globle from "@/common/global"
import Vue from 'vue'
export default ({
    namespaced:true,
    state: {
        PublishState:0, //0:关闭 1:导航 2:图文 3:视频 4:MD 5:草稿箱 6:发布
        PublishType:1, //1:发布 2:编辑
        LastInfo:{PublishState:0,PublishTitle:null}, //上一次
        PublishTitle:null,//标题
        PublishData:{},

        
    },
    getters: {
      KeepAlive(state){
         if(state.PublishState<=1) return []
         else return ['ImageText','MarkDown','Video']
      }
    },
    mutations: {

      SETPUBLISHTYPE:(state,index) => state.PublishType = index,
      //设置发布组件状态
      SETPUBLISHSTATE:(state,{index,title,data}) => {
        
 
        if(state.PublishState == 6){  
          state.LastInfo.PublishState =  1
          state.LastInfo.PublishTitle =  "发布博客"
        }else{
          state.LastInfo.PublishState =  state.PublishState
          state.LastInfo.PublishTitle =  state .PublishTitle
        }

        if(index == 6){
          state.PublishData = {...state.PublishData,...data}
        }else if(index <= 1){
          state.PublishData ={}
          state.PublishType = 1
        }
         
        if(state.PublishType == 2){
          state.PublishData = {...state.PublishData,...data}
        }
      
        state.PublishState=index || 0
        state.PublishTitle=title || '未定义'
   
      }       

    },
    actions: {
      async Publish(context,article){ //发布博客
           try{
              let res =await request.post("/api/article/add",article)
              return res.data
           }catch(error){return {message:error,code:400}}
       },
     async Edit(context,article){ //修改博客
        try{
           let res =await request.post("/api/article/edit",article)
           return res.data
        }catch(error){return {message:error,code:400}}
    },
      async LikeCollect(context,{uid,id,type}){ //点赞收藏
        try{
           let res =await request.get("/api/article/likecollect",{params:{uid,id,type}})
           return res.data
        }catch(error){return {message:error,code:400}}
     },
     async deleteArticle(context,id){ //删除
      try{
         let res =await request.get("/api/article/delete",{params:{id}})
         return res.data
       }catch(error){return {message:error,code:400}}
   },
     
     
    //
    }
})

